import { defaultOption } from "../runtimeLibs/common";

/**
 * 配置存储结构：
 {
   conf:{
     enable:true,
     optionData:{
 
     },
   }
 }
 */

export class DataStore {
  static getStore(key: string): any {
    const result = GM_getValue("conf", null);
    if (result && typeof result == typeof defaultOption[key]) {
      return result[key];
    } else {
      GM_setValue(key, defaultOption[key]);
      return defaultOption[key];
    }
  }

  static getGlobalEnabled() {
    return DataStore.getStore("enable");
  }

  static getConf(key: string): any {
    return DataStore.getStore("optionData")[key];
  }

  static setConf(key: string, value: any) {
    const raw = this.getStore("optionData");
    raw[key] = value;
    GM_setValue("optionData", raw);
  }

  static switchGlobalEnabled(state: boolean) {
    let raw = this.getStore("enable");
    raw = state ?? !raw;
    GM_setValue("enable", raw);
  }
}
